﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QLDV.Models;

namespace QLDV.Controllers
{
    public class CSDController : BaseController
    {
        //
        // GET: /CSD/

        public ActionResult Index()
        {
            int id = Int32.Parse(CurrentUser.TenDangNhap);
            CSD csd = Repository.LayDoanVien(id).CSD_DV2.Where(u=>u.TrangThai == true).First().CSD;
            return View(csd);
        }

        public ActionResult ViewInfo(int id)
        {
            CSD csd = Repository.LayCSD(id);
            TempData["id"] = id;
            
            return View(csd);
        }

        [HttpPost]
        public ActionResult ViewInfo(FormCollection values, CSD csd)
        {
            int id = (int) TempData["id"];
            string ntl = values["NgayThanhLap"];
            csd.NgayThanhLap = new DateTime(Int32.Parse(ntl.Substring(6, 4)), Int32.Parse(ntl.Substring(3, 2)), Int32.Parse(ntl.Substring(0, 2)));
            Repository.CapNhatCSD(id,csd);
            return RedirectToAction("ViewInfo", "CSD",id);
        }

        public ActionResult XemPhongTrao(int id)
        {
            CSD csd = Repository.LayCSD(id);
            return View(csd);
        }

        public ActionResult TaoPhongTrao(int id)
        {
            CSD csd = Repository.LayCSD(id);
            MANGHOATDONG mhd = Repository.LayMangHD();
            ViewBag.mhd = TaoDSMHD(mhd,-1,false);

            return View(csd);
        }

        private string TaoDSMHD(MANGHOATDONG mhd, int sd=0, bool isrender = true)
        {
            string t = "";
            for (int i = 0; i < sd; i++)
            {
                t += "--";
            }
            string rs = "";
            if (isrender)
            {
                rs += "<option value=" + mhd.Id_Manghd + ">" + t + mhd.TenMang + "</option>";
            }
            if (mhd.MANGHOATDONG1.Count == 0)
            {
                return rs;
            }
            sd++;
            foreach (MANGHOATDONG manghd in mhd.MANGHOATDONG1)
            {
                
                rs += TaoDSMHD(manghd, sd);
            }
            return rs;
        }
    }
}
